package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ahpu.propertymanage.dataclass.PageSet;
import edu.ahpu.propertymanage.pojo.Managers;
import edu.ahpu.propertymanage.pojo.PropertyTage;
import edu.ahpu.propertymanage.service.ManagersService;
import edu.ahpu.propertymanage.service.PropertyTageService;
import edu.ahpu.propertymanage.util.GetID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/managers")
public class ManagersController {

    @Autowired
    ManagersService managersService;

    @Autowired
    GetID getID;

    @Autowired
    PropertyTageService propertyTageService;

    @GetMapping("/addmanagers")
    public String addManagers() {
        return getID.createManagerUUID();
    }

    @PostMapping("/a&dmanagers")
    public String addorupdataManagers(@RequestBody Managers managers) {
        boolean bool = managersService.saveOrUpdate(managers);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("managerspage")
    public IPage<Managers> managerPage(@RequestBody PageSet pageSet) {
        IPage<Managers> managersIPage = new Page<>(pageSet.getCurrent(), pageSet.getSize());
        managersIPage = managersService.page(managersIPage);
        return managersIPage;
    }

    @PostMapping("deletemanagers")
    public String deleteManagers(String managersId) {
        boolean bool = managersService.removeById(managersId);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("detailmanagers")
    public Managers detailManager(String managersId){
        Managers managers = managersService.getById(managersId);
        return managers;
    }

    @PostMapping("searchmanagers")
    public List<Managers> searchWorker(String keyword){
        //正则表达式验证是否为手机号
        String regex = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))[0-9]{8}$";
        //创建一个条件（为了条件查询语句）
        QueryWrapper<Managers> wrapper = new QueryWrapper<>();
        //正则表达式查询结果
        boolean bool = keyword.matches(regex);
        //物业管理的表示，判断是否根据id查找
        PropertyTage propertyTage = propertyTageService.getById(1);
        //根据id查询
        if (keyword.length()>=4&&keyword.substring(0,4).equals(propertyTage.getTage())) {
            //sql语句(where workerId = keyword);
            wrapper.lambda().eq(Managers::getManagerId,keyword);
            List<Managers> managers = managersService.list(wrapper);
            return managers;
        }else if (bool){
            //sql语句(where workerPhone = keyword);
            wrapper.lambda().eq(Managers::getManagerPhone,keyword);
            List<Managers> managers = managersService.list(wrapper);
            return managers;
        }else {
            //sql语句(where workerName = keyword);
            wrapper.lambda().eq(Managers::getManagerName,keyword);
            List<Managers> managers = managersService.list(wrapper);
            return managers;
        }

    }

}
